package com.example.demo;

import com.ctrip.framework.apollo.Config;
import com.ctrip.framework.apollo.model.ConfigChange;
import com.ctrip.framework.apollo.model.ConfigChangeEvent;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfig;
import com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * Created by BG314005 on 2018/7/20.
 */
@RestController
public class TestApi2Controller {
    @ApolloConfig
    private Config config;

    @ApolloConfig("application2")
    private Config anotherConfig;

    @RequestMapping("/api2Hello")
    public String test() {
        String someKey = "test.string.hello";
        String someDefaultValue = "default";
        String value = config.getProperty(someKey, someDefaultValue);
        return value;
    }

    @RequestMapping("/api2Hello2")
    public String test2() {
        String someKey = "test.string.hello";
        String someDefaultValue = "default";
        String value = anotherConfig.getProperty(someKey, someDefaultValue);
        return value;
    }

    @ApolloConfigChangeListener("application2")
    public void configChangeListener(ConfigChangeEvent changeEvent) {
        if (changeEvent.isChanged("test.string.hello")) {
            String hello = config.getProperty("test.string.hello", "default");
            System.out.println(hello + "was changed.");
        }
    }
}
